// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ChronosWorkerWinMM.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

#pragma warning disable

#if ! AnyCPU

namespace Org.OpenTrader.Framework
{
    #region

    using System;
    using System.Runtime.InteropServices;

    #endregion

    #region Delegates

    /// <summary>
    /// The timer event handler.
    /// </summary>
    /// <param name="id">
    /// The id.
    /// </param>
    /// <param name="millisecondg">
    /// The millisecondg.
    /// </param>
    /// <param name="userCtx">
    /// The user ctx.
    /// </param>
    /// <param name="rsv1">
    /// The rsv 1.
    /// </param>
    /// <param name="rsv2">
    /// The rsv 2.
    /// </param>
    public delegate void TimerEventHandler(uint id, uint millisecondg, ref int userCtx, int rsv1, int rsv2);

    #endregion

    /// <summary>
    /// The trtr.
    /// </summary>
    [CLSCompliant(false)]
    public class ChronosWorkerWinMM : AbtractChronosWorker
    {
        #region Constants and Fields

        /// <summary>
        /// The handler.
        /// </summary>
        private readonly TimerEventHandler handler;

        /// <summary>
        /// The data.
        /// </summary>.
        private int data;

        /// <summary>
        /// The timer.
        /// </summary>
        private uint timer;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChronosWorkerWinMM"/> class.
        /// </summary>
        /// <param name="resolution">
        /// The Resolution.
        /// </param>
        /// <param name="precision">
        /// The precision.
        /// </param>
        public ChronosWorkerWinMM()
            : base(10, 5)
        {
            this.handler = this.OnWinMMNativeTimer;
            this.data = 0;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The start.
        /// </summary>
        public override void Start()
        {
            if (this.timer == 0)
            {
                this.timer = timeSetEvent(this.Resolution, 0, this.handler, ref this.data, 1 /* cyclic */);
            }
        }

        /// <summary>
        /// The stop.
        /// </summary>
        public override void Stop()
        {
            if (this.timer != 0)
            {
                timeKillEvent(this.timer);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The time kill event.
        /// </summary>
        /// <param name="timerEventId">
        /// The timer event id.
        /// </param>
        /// <returns>
        /// The time kill event.
        /// </returns>
        [DllImport("WinMM.dll", SetLastError = true)]
        private static extern uint timeKillEvent(uint timerEventId);

        /// <summary>
        /// The time set event.
        /// </summary>
        /// <param name="millisecondDelay">
        /// The millisecond delay.
        /// </param>
        /// <param name="millisecondResolution">
        /// The millisecond Resolution.
        /// </param>
        /// <param name="handler">
        /// The handler.
        /// </param>
        /// <param name="userCtx">
        /// The user ctx.
        /// </param>
        /// <param name="eventType">
        /// The event type.
        /// </param>
        /// <returns>
        /// The time set event.
        /// </returns>
        [DllImport("WinMM.dll", SetLastError = true)]
        private static extern uint timeSetEvent(
            int millisecondDelay, int millisecondResolution, TimerEventHandler handler, ref int userCtx, int eventType);

        /// <summary>
        /// The on win mm native timer.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="millisecondg">
        /// The millisecondg.
        /// </param>
        /// <param name="userCtx">
        /// The user ctx.
        /// </param>
        /// <param name="rsv1">
        /// The rsv 1.
        /// </param>
        /// <param name="rsv2">
        /// The rsv 2.
        /// </param>
        private void OnWinMMNativeTimer(uint id, uint millisecondg, ref int userCtx, int rsv1, int rsv2)
        {
            this.OnTimer();
        }

        #endregion
    }
}

#endif